using System.Collections;
using System.IO;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
// using System;

namespace YF.Art
{
    public class DTCheckPrefab : EditorWindow
    {
        List<Object> obj = new List<Object>();
        Vector2 scrollView;
        int winhight = 500;
        List<UnityEngine.Object> _pendingRenamePaths = new List<UnityEngine.Object>();
        List<UnityEngine.Object> _WorkObj = new List<UnityEngine.Object>();
        List<UnityEngine.Object> _OutWorkObj = new List<UnityEngine.Object>();

        List<UnityEngine.Object> date = new List<UnityEngine.Object>();
        [MenuItem("DT Tools /Check Prefab  Size")]
        private static void ShowWindow()
        {
            var window = GetWindow<DTCheckPrefab>();
            window.titleContent = new GUIContent("Check Prefab ");
            window.Show();
        }

        private void OnGUI()
        {
            if (GUILayout.Button("load obj"))
            {
                obj.Clear();
                string[] go = Selection.assetGUIDs;
                foreach (string g in go)
                {
                    Object objaa = AssetDatabase.LoadAssetAtPath<Object>(AssetDatabase.GUIDToAssetPath(g));
                    if (objaa == null) continue;
                    obj.Add(objaa);
                }
            }
            winhight = (int)this.position.height - 130;
            foreach (var item in obj)
            {
                EditorGUILayout.ObjectField("path", item, typeof(GameObject), false);
            }



            if (GUILayout.Button("Check"))  // 检查按钮
            {
                string basePath;
                basePath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(obj[0]));

                _pendingRenamePaths.Clear();
                _WorkObj.Clear();
                _OutWorkObj.Clear();
                foreach (var item in obj)
                {
                    if (item != null)
                    {
                        CheckGUIDwork(item);
                    }
                }
                string[] guids = AssetDatabase.FindAssets("", new string[] { basePath });
                foreach (string guid in guids)  // 遍历所有纹理
                {
                    string assetPath = AssetDatabase.GUIDToAssetPath(guid);  // 获取纹理路径
                    var o = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(assetPath);  // 获取纹理导入器
                    if (o == null) continue;  // 如果导入器为空，跳过
                    if (_WorkObj.Contains(o)) continue;  // 如果导入器为空，跳过
                    if (IsCommon(assetPath)) continue;  // 如果导入器为空，跳过
                    if (o.GetType().ToString() == "UnityEditor.DefaultAsset") continue;  // 如果导入器为空，跳过
                    if (o.GetType().ToString() == "UnityEngine.MonoScript") continue;  // 如果导入器为空，跳过 
                    _OutWorkObj.Add(o);  // 标记需要重命名的纹理
                }
            }

            if (_pendingRenamePaths.Count > 0)
            {
                EditorGUILayout.LabelField("需要检查的对象：");  // 显示需要重命名的纹理列表 
                scrollView = EditorGUILayout.BeginScrollView(scrollView, GUILayout.Height(winhight * 0.5f));
                foreach (var obj in _pendingRenamePaths)
                {
                    EditorGUILayout.ObjectField(obj, typeof(UnityEngine.Object), false);  // 显示纹理对象
                }
                EditorGUILayout.EndScrollView();
            }
            EditorGUILayout.LabelField("out");
            if (_OutWorkObj.Count > 0)
            {
                EditorGUILayout.LabelField("需要检查的对象：");  // 显示需要重命名的纹理列表 
                scrollView = EditorGUILayout.BeginScrollView(scrollView, GUILayout.Height(winhight * 0.5f));
                foreach (var obj in _OutWorkObj)
                {
                    // AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(obj));
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.ObjectField(obj, typeof(UnityEngine.Object), false);  // 显示纹理对象
                    if (GUILayout.Button("D", GUILayout.MaxWidth(20)))
                    {
                        AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(obj));
                        AssetDatabase.Refresh();
                    }
                    EditorGUILayout.EndHorizontal();
                }
                EditorGUILayout.EndScrollView();
            } 
        }

        void CheckGUIDwork(UnityEngine.Object go)
        {
            string basePath;
            basePath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(go));
            //获取对象数组
            string[] objects = AssetDatabase.GetDependencies(AssetDatabase.GetAssetPath(go));

            //按地址把这些对象复制到新目录
            foreach (var g in objects)
            {
                string op = Path.GetDirectoryName(g);
                var o = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(g);
                _WorkObj.Add(o);
                if (op.StartsWith(basePath)) continue;
                if (IsCommon(g)) continue;
                _pendingRenamePaths.Add(o);
            }



            //     ObjectData d = new ObjectData();
            //     d.oldGUID = AssetDatabase.AssetPathToGUID(g);
            //     if (IsCommon(g, isCopyCommon)) continue;

            //     string n = Path.GetFileName(g);
            //     var aaa = AssetDatabase.LoadAssetAtPath<Object>(g);
            //     string np = newBasePath + "/" + n;
            //     string t = aaa.GetType().ToString();
            //     switch (t)
            //     {
            //         case "UnityEditor.MonoScript":
            //             break;
            //         case "UnityEngine.Texture2D":
            //             np = newBasePath + "/Textures/" + n;
            //             // creatNewFolder(newBasePath, "Textures");
            //             break;
            //         case "UnityEngine.Material":
            //             np = newBasePath + "/Materials/" + n;
            //             // creatNewFolder(newBasePath, "Materials");
            //             break;
            //         case "UnityEditor.Animations.AnimatorController":
            //             np = newBasePath + "/Animations/" + n;
            //             // creatNewFolder(newBasePath, "Animations");
            //             break;
            //         case "UnityEngine.AnimatorOverrideController":
            //             np = newBasePath + "/Animations/" + n;
            //             // creatNewFolder(newBasePath, "Animations");
            //             break;
            //         case "UnityEngine.AnimationClip":
            //             np = newBasePath + "/Animations/" + n;
            //             // creatNewFolder(newBasePath, "Animations");
            //             break;
            //         case "UnityEngine.GameObject":   //Models
            //             string exname = Path.GetExtension(g).ToLower();
            //             if (exname == ".fbx" || exname == ".obj")
            //             {
            //                 // creatNewFolder(newBasePath, "Models");
            //                 np = newBasePath + "/Models/" + n;
            //             }
            //             else
            //             {
            //                 np = newBasePath + "/" + n;
            //             }
            //             break;
            //         case "UnityEngine.Mesh":
            //             // creatNewFolder(newBasePath, "Models");
            //             np = newBasePath + "/Models/" + n;
            //             break;

            //         default:
            //             np = newBasePath + "/" + n;
            //             break;
            //     }
            //     np = np.Replace("\\", "/");
            //     var tmpobj = AssetDatabase.LoadAssetAtPath(np, typeof(UnityEngine.Object));
            //     if (tmpobj != null)
            //     {
            //         d.newGUID = AssetDatabase.AssetPathToGUID(np);
            //     }
            //     else
            //     {
            //         if (AssetDatabase.CopyAsset(g, np))
            //         {
            //             d.newGUID = AssetDatabase.AssetPathToGUID(np);
            //         }
            //     }
            //     data.Add(d);
            // }

            // foreach (var t in data)
            // {
            //     string fp = AssetDatabase.GUIDToAssetPath(t.newGUID);
            //     string tm = Path.GetExtension(fp).ToLower();
            //     if (tm == ".fbx" || tm == ".png" || tm == ".jpg" || tm == ".tga")
            //         continue;
            //     if (tm == ".shader")
            //         continue;
            //     if (tm == ".cs")
            //         continue;
            //     filework(fp, data);
            // }
            AssetDatabase.Refresh();
        }

        bool IsCommon(string path)
        {
            if (path.ToLower().IndexOf("common") >= 0)
            {
                Debug.LogWarning("这个 " + path + " 对象存在于Common目录下,被忽略!");
            }
            bool a = path.ToLower().IndexOf("common") > 0;

            if (a)
            {
                var g = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(path);
                string t = g.GetType().ToString();
                a = false;
                if (t == "UnityEngine.Texture2D")
                {
                    a = true;
                }
                if (t == "UnityEngine.GameObject")
                {
                    string exname = Path.GetExtension(path).ToLower();
                    if (exname == ".fbx" || exname == ".obj")
                    {
                        a = true;
                    }
                }
            }
            bool b = Path.GetExtension(path).ToLower() == ".shader";

            bool c = Path.GetExtension(path).ToLower() == ".cs";
            // return path.ToLower().IndexOf("common") > 0;
            return a || b || c;
        }
    }
}
